package com.huiminpay.merchant.controller;

import com.huiminpay.merchant.common.util.SecurityUtil;
import com.huiminpay.transaction.api.PayChannelService;
import com.huiminpay.transaction.dto.PayChannelDTO;
import com.huiminpay.transaction.dto.PayChannelParamDTO;
import com.huiminpay.transaction.dto.PlatformChannelDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yz
 * @Date: 2024/7/23 14:26
 */
@Api(value = "商户平台-渠道和支付参数相关", tags = "商户平台-渠道和支付参数", description = "商户平台-渠道和支付参数相关")
@Slf4j
@RestController
public class PayChannelController {
    @Reference
    private PayChannelService payChannelService;


    @ApiOperation("获取平台服务类型")
    @GetMapping(value="/my/platform-channels")
    public List<PlatformChannelDTO> queryPlatformChannel(){
        return payChannelService.queryPlatFormChannel();
    }

    @ApiOperation("绑定服务类型")
    @PostMapping(value="/my/apps/{appId}/platform-channels")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value="应用id",required=true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name="platformChannelCodes",value = "平台服务类型",required = true,dataType = "String",paramType = "query")
    })
    public void bindPlatformChannelForApp(@PathVariable("appId") String appId,
                                          @RequestParam("platformChannelCodes") String platformChannelCodes){
        payChannelService.bindPlatformChannelForApp(appId,platformChannelCodes);
    }

    @ApiOperation("查询应用是否绑定了某个服务类型")
    @GetMapping("/my/merchants/apps/platformchannels")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId",value="应用id",required=true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name="platformChannel",value = "服务类型",required = true,dataType = "String",paramType = "query")
    })
    public int queryAppBindPlatformChannel(@RequestParam("appId") String appId,
                                            @RequestParam("platformChannel") String platformChannel){
       return payChannelService.queryAppBindPlatformChannel(appId,platformChannel);
    }

    @ApiOperation("查询服务类型下的支付渠道")
    @GetMapping(value="/my/pay-channels/platform-channel/{platformChannelCode}")
    @ApiImplicitParam(name = "platformChannelCode", value = "服务类型编码", required =
                    true, dataType = "String", paramType = "path")

    public List<PayChannelDTO> queryPayChannelByPlatformChannel(@PathVariable("platformChannelCode") String platformChannelCode){
        return payChannelService.queryPayChannelByPlatformChannel(platformChannelCode);
    }

    @ApiOperation("商户配置支付渠道参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payChannelParam", value = "商户配置支付渠道参数",
                    required = true, dataType = "PayChannelParamDTO", paramType = "body")
    })
    @RequestMapping(value = "/my/pay-channel-params",
            method = {RequestMethod.POST,RequestMethod.PUT})
    public void createPayChannelParam(@RequestBody PayChannelParamDTO payChannelParam){
        Long merchantId = SecurityUtil.getMerchantId();
        payChannelParam.setMerchantId(merchantId);
        payChannelService.createPayChannelParam(payChannelParam);
    }

    @ApiOperation("根据应用和服务类型获取原始支付参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appId", value = "应用id", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "platformChannel", value = "服务类型", required = true, dataType = "String", paramType = "path")})
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}")
    public List<PayChannelParamDTO> queryPayChannelParam(@PathVariable String appId, @PathVariable String platformChannel) {
        return payChannelService.queryPayChannelParamByAppIdAndPlatformChannelCode(appId, platformChannel);
    }


    @ApiOperation("根据应用、服务类型及支付渠道获取原始支付参数")
    @GetMapping(value = "/my/pay-channel-params/apps/{appId}/platform-channels/{platformChannel}/pay-channels/{payChannel}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="appId",value="应用id",required=true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name="platformChannel",value = "服务类型",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name="payChannel",value = "支付渠道",required = true,dataType = "String",paramType = "path")
    })
    public PayChannelParamDTO queryPayChannelParam(@PathVariable("appId") String appId,
                                                   @PathVariable("platformChannel") String platformChannel,
                                                   @PathVariable("payChannel") String payChannel){
        PayChannelParamDTO payChannelParamDTO = payChannelService.queryPayChannelParamByAppIdAndPlatformChannelCodeAndPayChannelCode(appId, platformChannel, payChannel);
        return payChannelParamDTO;
    }


}
